package com.jiandian.service.impl;


import com.jiandian.dao.SignPrizeMapper;
import com.jiandian.entity.SignPrize;
import com.jiandian.entity.SignUser;
import com.jiandian.page.PageInfo;
import com.jiandian.page.WhereMapUtil;
import com.jiandian.service.SignPrizeService;
import com.jiandian.util.BeanUtils;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.List;

/**
 * 奖品
 *
 * @author houqi Fu
 * @date 2018/12/15
 */
@Service
@Transactional
public class SignPrizeServiceImpl implements SignPrizeService {

    private static final Logger logger = LoggerFactory.getLogger(SignPrizeServiceImpl.class);

    @Autowired
    private SignPrizeMapper mapper;

    @Override
    public SignPrize add(SignPrize record) {

        initBasicValue(record);

        int result = mapper.insert(record);
        if (result > 0) {
            return record;
        }
        return null;
    }

    @Override
    public SignPrize update(SignPrize record) {
        Integer id = record.getId();
        if (id == null) {
            return null;
        }
        SignPrize dbSignPrize = mapper.selectByPrimaryKey(id);
        if (dbSignPrize == null) {
            return null;
        }
        BeanUtils.copyProperties(record, dbSignPrize);
        // dbSignPrize.setGmtModified(new Timestamp(System.currentTimeMillis()));
        int result = mapper.updateByPrimaryKey(dbSignPrize);
        if (result > 0){
            return dbSignPrize;
        }
        return null;
    }

    @Override
    public SignPrize findById(Integer id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public List<SignPrize> findAll(PageInfo pageInfo) {
        return mapper.selectAll(pageInfo);
    }

    @Override
    public int selectCount(PageInfo pageInfo) {
        return mapper.selectCount(pageInfo);
    }

    @Override
    public int batchInsert(List<SignPrize> list) {
        for (SignPrize bean : list) {
            initBasicValue(bean);
        }
        return mapper.batchInsert(list);
    }

    @Override
    public List<SignPrize> batchQueryByIds(List<Integer> ids) {
        return mapper.batchQueryByIds(ids);
    }

    @Override
    public int delete(Integer id) {
        return mapper.deleteByPrimaryKey(id);
    }

    @Override
    public int delete(SignPrize record) {
        return delete(record.getId());
    }

    @Override
    public int batchDeleteById(List<Integer> ids) {
        if (CollectionUtils.isNotEmpty(ids)) {
            return mapper.batchDeleteById(ids);
        }
        return 0;
    }

    private void initBasicValue(SignPrize record){
        Timestamp now = new Timestamp(System.currentTimeMillis());
        // record.setGmtCreate(now);
        // record.setGmtModified(now);
    }


    @Override
    public PageInfo getPage(SignPrize whereEntity, PageInfo pageInfo) {
        pageInfo.setWhereMap(WhereMapUtil.convertBeanToWhereMap(whereEntity));
        pageInfo.setTotalRecord(selectCount(pageInfo));
        pageInfo.queryPageResult(pageInfo);
        pageInfo.setResultObject(findAll(pageInfo));
        return pageInfo;
    }
}
